/*
 * File: ftktab.cpp
 * Author:  Jiao JinXing <jiaojinxing1987@gmail.com>
 * Brief:   ftk tab.
 *
 * Copyright (c) 2010 - 2011  Jiao JinXing <jiaojinxing1987@gmail.com>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * History:
 * ================================================================
 * Jiao JinXing <jiaojinxing1987@gmail.com> created
 *
 */

#include "ftktab.h"
#include "ftkbutton.h"
#include "ftkwindow.h"
#include "mainwindow.h"

#include <QtGui>

FtkTab::FtkTab(WidgetTree *tree, QDomElement &element, QTreeWidgetItem *item, QWidget *parent) :
        QTabWidget(parent)
{
    this->item = item;
    this->tree = tree;
    this->element = element;

    if (!element.hasAttribute("w")) {
        if (curDesignWindow->widgetSizeRelatively) {
            element.setAttribute("w", "$pw/2");
        } else {
            element.setAttribute("w", "240");
        }
    }

    if (!element.hasAttribute("h")) {
        if (curDesignWindow->widgetSizeRelatively) {
            element.setAttribute("h", "$ph/2");
        } else {
            element.setAttribute("h", "160");
        }
    }

    int x, y, w, h;
    FtkXul xul;
    x = ftk_expr_eval(xul.ftk_preprocess_value(this, element.attribute("x").toAscii().data()));
    y = ftk_expr_eval(xul.ftk_preprocess_value(this, element.attribute("y").toAscii().data()));
    w = ftk_expr_eval(xul.ftk_preprocess_value(this, element.attribute("w").toAscii().data()));
    h = ftk_expr_eval(xul.ftk_preprocess_value(this, element.attribute("h").toAscii().data()));
    setGeometry(x, y, w, h);

    tabCount = 0;
    QWidget *firstTab = new QWidget(this);
    QString text;
    text.sprintf("tab%d", tabCount++);
    addTab(firstTab, text);
    setAcceptDrops(true);
    show();
}

FtkTab::~FtkTab()
{
    if (curDesignWindow->widgetsRemoving)
        tree->deleteItemElement(item, element);
}

void FtkTab::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
}

void FtkTab::dragLeaveEvent(QDragLeaveEvent *event)
{
    event->accept();
}

void FtkTab::dragMoveEvent(QDragMoveEvent *event)
{
    event->accept();
}

void FtkTab::dropEvent(QDropEvent *event)
{
    event->accept();
}

void FtkTab::mousePressEvent(QMouseEvent *event)
{
    if (!MainWindow::ctrlPress) {
        IGNORE_CTRL:
        if (event->button() == Qt::LeftButton) {
            dragPosition = event->globalPos() - frameGeometry().topLeft();
            tree->setCurrentItem(item);
        }
    } else {
        if (curDesignWindow->setMainWidget(this) == false) {
            goto IGNORE_CTRL;
        }
    }
    event->accept();
}

void FtkTab::mouseMoveEvent(QMouseEvent *event)
{
    if (!MainWindow::ctrlPress) {
        if (event->buttons() == Qt::LeftButton) {
            QPoint newPos = event->globalPos() - dragPosition;

            if (newPos.x() < 0) {
                newPos.setX(0);
            }
            if (newPos.y() < 0) {
                newPos.setY(0);
            }
            if (newPos.x() + width() >= parentWidget()->width()) {
                newPos.setX(parentWidget()->width() - width() - 1);
            }
            if (newPos.y() + height() >= parentWidget()->height()) {
                newPos.setY(parentWidget()->height() - height()- 1);
            }
            move(newPos);

            if (curDesignWindow->widgetSizeRelatively) {
                QString pos;
                pos.sprintf("$pw*%d/%d", x(), parentWidget()->width());
                element.setAttribute("x", pos);
                pos.sprintf("$ph*%d/%d", y(), parentWidget()->height());
                element.setAttribute("y", pos);
            } else {
                element.setAttribute("x", x());
                element.setAttribute("y", y());
            }

            tree->somethingChanged();
        }
    }

    event->accept();
}
